#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define size 10

void InitBoard(char (*board)[size])
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            board[i][j] = '-';
        }
    }
}
void PrintBoard(char (*board)[size])
{
    printf("     ");
    for (int i = 0; i < size; i++)
    {
        printf("%4d ", i + 1);
    }
    printf("\n");
    for (int i = 0; i < size; i++)
    {
        printf("%4d ", i + 1);
        for (int j = 0; j < size; j++)
        {
            printf("%4c ", board[i][j]);
        }
        printf("\n");
    }
}
bool MakeMove(char (*board)[size], int row, int column, char player)
{
    if (row < 0 || row >= size || column < 0 || column >= size)
    {
        printf("非法下棋位置，请重新输入。\n");
        return false;
    }
    if (board[row][column] != '-')
    {
        printf("该位置已经有棋子了，请重新输入。\n");
        return false;
    }
    board[row][column] = player;
    return true;
}
bool IsValidPlace(int row, int column)
{
    if (row < 0 || row >= size || column < 0 || column >= size)
    {
        return false;
    }
    return true;
}
bool CheckIsWin(char (*board)[size], int row, int column, char player)
{
    int direction[4][2] =
        {
            {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
    // 遍历四个方向数棋子数量
    for (int i = 0; i < 4; i++)
    {
        int count = 1;
        // 下一颗棋子的位置
        int dex = row + direction[i][0];
        int dey = column + direction[i][1];
        while (IsValidPlace(dex, dey) && board[dex][dey] == player)
        {
            count++;
            if (count >= 5)
            {
                return true;
            }
            dex = dex + direction[i][0];
            dey = dey + direction[i][1];
        }
        dex = row - direction[i][0];
        dey = column - direction[i][1];
        while (IsValidPlace(dex, dey) && board[dex][dey] == player)
        {
            count++;
            if (count >= 5)
            {
                return true;
            }
            dex = dex - direction[i][0];
            dey = dey - direction[i][1];
        }
    }
    return false;
}
bool CheckIsDraw(char (*board)[size], char player)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (board[i][j] == '-')
            {
                return false;
            }
        }
    }
    return true;
}
bool AIMakeMove(char (*board)[size], char player, char ai, int PlayerRow, int PlayerColumn)
{
    // 优先取胜策略
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (board[i][j] == '-')
            {
                board[i][j] = ai;
                if (CheckIsWin(board, i, j, ai) == true)
                {
                    return true;
                }
                board[i][j] = '-';
            }
        }
    }
    // 阻止对手策略
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (board[i][j] == '-')
            {
                board[i][j] = player;
                if (CheckIsWin(board, i, j, ai) == true)
                {
                    board[i][j] = ai;
                    return true;
                }
                board[i][j] = '-';
            }
        }
    }
    int direction[8][2] =
        {
            {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
    // 随机数的产生由随机种子决定，随机种子不变，随机数就不会变
    // 以当前时间作为随机种子

    while (1)
    {
        srand(time(NULL));
        int i = rand() % 8;
        int dx = PlayerRow + direction[i][0];
        int dy = PlayerColumn + direction[i][1];
        if (IsValidPlace(dx, dy) && board[dx][dy] == '-')
        {
            board[dx][dy] = ai;
            return true;
        }
    }
    return false;
}
int main()
{
    char board[size][size] = {0};
    InitBoard(board);
    char player = 'X';
    int row = 0, column = 0;
    while (1)
    {
        PrintBoard(board);
        printf("请|%c|选手下棋：", player);
        if (player == 'X')
        {
            scanf("%d %d", &row, &column);
            if (MakeMove(board, row - 1, column - 1, player) == false)
            {
                continue;
            }
        }
        else
        {
            printf("ai正在下棋\n");
            if (AIMakeMove(board, 'X', 'O', row - 1, column - 1) == false)
            {
                continue;
            }
        }
        if (CheckIsWin(board, row - 1, column - 1, player) == true)
        {
            PrintBoard(board);
            printf("恭喜|%c|选手获胜\n", player);
            break;
        }
        if (CheckIsDraw(board, player) == true)
        {
            PrintBoard(board);
            printf("平局\n");
            break;
        }
        player = (player == 'X' ? 'O' : 'X');
    }
    return 0;
}